package z_app_lotto2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;

public class LottoMain2 extends JFrame {
	static int totListFrame_width=300;
	static int totListFrame_height=400;
	static int totListFrame_top=30;
	static int totListFrame_left=30;
	
	String fileName = "src/z_app_lotto2/lotto.dat";
	Vector<String> data;
	
	TotListFrame totListFrame = null;
	MaxNumber maxNumber = null;
	Term term = null;
	
	
	private JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenu mnNewMenu_1;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem menuItem;
	private JMenuItem menuItem_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LottoMain2 frame = new LottoMain2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LottoMain2() {
		setTitle("Lotto Ver 0.8");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 732, 678);
		
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		loadFile();
	}
	public void loadFile(){
		Data d = new Data(fileName);
		if(data !=null) data.removeAllElements();
		data = d.getData();
	}
	

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
			menuBar.add(getMnNewMenu_1());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("파일");
			mnNewMenu.add(getMntmNewMenuItem());
		}
		return mnNewMenu;
	}
	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("시뮬레이션");
			mnNewMenu_1.add(getMntmNewMenuItem_1());
			mnNewMenu_1.add(getMenuItem());
			mnNewMenu_1.add(getMenuItem_1());
		}
		return mnNewMenu_1;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("데이터 읽기");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					loadFile();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("전체목록");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(totListFrame == null){
					
						totListFrame = new TotListFrame(data);
						
						totListFrame.addInternalFrameListener(new InternalFrameAdapter() {
							@Override
							public void internalFrameClosing(InternalFrameEvent e) {
								totListFrame = null;
							}
							
						});
						
						contentPane.add(totListFrame);
						//contentPane.updateUI();

					}
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMenuItem() {
		if (menuItem == null) {
			menuItem = new JMenuItem("최대발생번호");
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(maxNumber != null) return;
					maxNumber = new MaxNumber(data);
					maxNumber.addInternalFrameListener(new InternalFrameAdapter() {
						@Override
						public void internalFrameClosing(InternalFrameEvent e) {
							maxNumber = null;
						}
						
					});
					
					contentPane.add(maxNumber);
					//contentPane.updateUI();
				}
			});
		}
		return menuItem;
	}
	private JMenuItem getMenuItem_1() {
		if (menuItem_1 == null) {
			menuItem_1 = new JMenuItem("발생빈도");
			menuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(term != null) return;
					term = new Term();
					term.addInternalFrameListener(new InternalFrameAdapter() {
						@Override
						public void internalFrameClosing(InternalFrameEvent e) {
							term = null;
						}
						
					});
					
					contentPane.add(term);
					contentPane.updateUI();
				}
			});
		}
		return menuItem_1;
	}
}
